package com.ruoyi.wms.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.wms.domain.WmsMaterialInfo;
import com.ruoyi.wms.domain.WmsRepairSummary;
import com.ruoyi.wms.domain.WmsScrapSummary;
import com.ruoyi.wms.mapper.WmsMaterialInfoMapper;
import com.ruoyi.wms.service.IWmsRepairSummaryService;
import com.ruoyi.wms.service.IWmsScrapSummaryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.mapper.WmsInListMapper;
import com.ruoyi.wms.domain.WmsInList;
import com.ruoyi.wms.service.IWmsInListService;
import com.ruoyi.common.core.text.Convert;

/**
 * 入库记录Service业务层处理
 * 
 * @author yy
 * @date 2024-02-15
 */
@Service
public class WmsInListServiceImpl implements IWmsInListService 
{
    @Autowired
    private WmsInListMapper wmsInListMapper;
    @Autowired
    private WmsMaterialInfoMapper wmsMaterialInfoMapper;

    @Autowired
    private IWmsRepairSummaryService wmsRepairSummaryService;
    @Autowired
    private IWmsScrapSummaryService wmsScrapSummaryService;
    /**
     * 查询入库记录
     * 
     * @param id 入库记录主键
     * @return 入库记录
     */
    @Override
    public WmsInList selectWmsInListById(Long id)
    {
        return wmsInListMapper.selectWmsInListById(id);
    }

    /**
     * 查询入库记录列表
     * 
     * @param wmsInList 入库记录
     * @return 入库记录
     */
    @Override
    public List<WmsInList> selectWmsInListList(WmsInList wmsInList)
    {
        return wmsInListMapper.selectWmsInListList(wmsInList);
    }
    @Override
    public WmsInList selectWmsInListCount( ){
        return wmsInListMapper.selectWmsInListCount();
    }
    /**
     * 新增入库记录
     * 
     * @param wmsInList 入库记录
     * @return 结果
     */
    @Override
    public int insertWmsInList(WmsInList wmsInList)
    {

        for(WmsInList item :wmsInList.getItemList()){
            item.setCreateTime(DateUtils.getNowDate());
            item.setCreateBy(ShiroUtils.getSysUser().getUserName());
            item.setInTime(new Date());
            item.setUserId(ShiroUtils.getUserId());
            wmsInListMapper.insertWmsInList(item);
            //增加入库数量
            WmsMaterialInfo mater = new WmsMaterialInfo();
            mater.setCode(item.getCode());
            mater.setTotal(item.getInQuantity());
            wmsMaterialInfoMapper.updateWmsMaterialTotal(mater);
            //增加报修数量
            if(item.getRepairQuantity()!=null && item.getRepairQuantity().doubleValue()!=0){
                WmsRepairSummary repair = new WmsRepairSummary();
                BeanUtils.copyProperties(item,repair);
                repair.setRepairQuantity(item.getRepairQuantity());
                wmsRepairSummaryService.insertWmsRepairSummary(repair);
            }

            //增加报废数量
            if(item.getScrapQuantity()!=null && item.getScrapQuantity().doubleValue()!=0){
                WmsScrapSummary scrapSummary = new WmsScrapSummary();
                BeanUtils.copyProperties(item,scrapSummary);
                scrapSummary.setScrapQuantity(item.getScrapQuantity());
                wmsScrapSummaryService.insertWmsScrapSummary(scrapSummary);
            }


        }
        return 1;
    }

    /**
     * 修改入库记录
     * 
     * @param wmsInList 入库记录
     * @return 结果
     */
    @Override
    public int updateWmsInList(WmsInList wmsInList)
    {
        wmsInList.setUpdateTime(DateUtils.getNowDate());
        return wmsInListMapper.updateWmsInList(wmsInList);
    }

    /**
     * 批量删除入库记录
     * 
     * @param ids 需要删除的入库记录主键
     * @return 结果
     */
    @Override
    public int deleteWmsInListByIds(String ids)
    {
        return wmsInListMapper.deleteWmsInListByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除入库记录信息
     * 
     * @param id 入库记录主键
     * @return 结果
     */
    @Override
    public int deleteWmsInListById(Long id)
    {
        return wmsInListMapper.deleteWmsInListById(id);
    }
}
